import axios from 'axios'

const URL = 'http://127.0.0.1:9981/'

// 创建axios实例
let instance = axios.create({
    baseURL: URL,
    timeout: 10000
})

// 请求拦截器
instance.interceptors.request.use(config => {
    return config
}, error => {
    // 对请求错误的逻辑
    return Promise.reject(error)
})

// 响应拦截器
instance.interceptors.response.use(response => {
    return response.data
}, error => {
    console.log('响应拦截器报错, err: {}', error)
    return Promise.reject(error)
})

export default (method, url, data = {}) => {
    if (method === 'GET') {
        return instance.get(url, {params: data})
    } else if (method === 'POST') {
        return instance.post(url, data)
    } else if (method === 'PUT') {
        return instance.put(url, data)
    } else if (method === 'DELETE') {
        return instance.delete(url, {params: data})
    } else {
        console.error('未知的method: {}', method)
        return false
    }
}
